package org.bminfo.migle.format;

import java.util.ArrayList;
import java.util.List;

public class GenBank {
	public String[] genbankParser(String content, String region) {

		List cdsList = new ArrayList();
		String[] contentArr = content.split("\n");
		String[] arr = null;
		String line = "";
		String accn = "";
		String gi = "";
		String mRNA_seq = "";
		String seq_region = "";

		StringBuilder sbSeq = null; // sequence
		StringBuilder fasta = new StringBuilder(); // sequence

		int cds_start = 0;
		int cds_end = 0;
		int cds_length = 0;
		int length = 0;
		int curEnd = 0;
		boolean seqBegin = false;
		String temp = "";

		for (int i = 0; i < contentArr.length; i++) {
			line = contentArr[i];
			if (line.trim().length() > 0) {

				if (line.trim().toLowerCase().startsWith("cds")) {

					cdsList.add(line.trim());

				} else if(line.trim().toUpperCase().startsWith("ACCESSION")){
					
					arr = line.trim().split(" ");
					accn = arr[arr.length-1];
					
				} else if (line.trim().toLowerCase().startsWith("version")) {

					arr = line.trim().split(" ");
					for (int j = 0; j < arr.length; j++) {
						temp = arr[j].trim();
						if (temp.toLowerCase().startsWith("gi:")) {
							gi = temp.substring(3, temp.length());
						}
					}
				} else if (line.toLowerCase().startsWith("origin")) {
					seqBegin = true;
					sbSeq = new StringBuilder();
					continue;
				}
				if (seqBegin && !line.startsWith("//")) {

					temp = line.substring(10, line.length());
					temp = temp.replace(" ", "");
					sbSeq.append(temp.trim());
				}
			}// end if line > 0
		}// end for
		if (cdsList.size() == 1) {

			cds_start = 0;
			cds_end = 0;
			temp = (String) cdsList.get(0);
			temp = temp.substring(4, temp.length()).trim();
			arr = temp.split("\\.\\.");
			cds_start = Integer.parseInt(arr[0]);
			cds_end = Integer.parseInt(arr[1]);
			cds_length = cds_end - cds_start + 1;
			
		} else if (cdsList.size() > 1) {

			cds_start = 0;
			cds_end = 0;
			
			for (int i = 0; i < cdsList.size(); i++) {

				temp = (String) cdsList.get(i);
				temp = temp.substring(4, temp.length()).trim();
				arr = temp.split("\\.\\.");
				curEnd = Integer.parseInt(arr[1]);
				if (curEnd > cds_end) {
					cds_start = Integer.parseInt(arr[0]);
					cds_end = Integer.parseInt(arr[1]);
				}
			}
//			cds_length = cds_end - cds_start + 1;
		}
//		System.out.println(sbSeq.toString().length());

		mRNA_seq = sbSeq.toString().toUpperCase().replace("\r","");
		length = mRNA_seq.length();
		
		arr = new String[3];
		
		if(region.equals("3utr")){
			
			seq_region = mRNA_seq.substring(cds_end, length);
			fasta.append(">gi|").append(gi).append("|miGLE|3'UTR|Start=");
			fasta.append((cds_length+1)).append("|Length=").append(seq_region.length());
			fasta.append("|CodonStart|1|CdsLength|").append(cds_length).append("\n");
			
			arr[0] = Integer.toString(cds_length+1);
			arr[1] = Integer.toString(cds_length+seq_region.length());
			
		} else if (region.equals("5utr")){
			
			seq_region = mRNA_seq.substring(0,cds_start-1);
			fasta.append(">gi|").append(gi).append("|miGLE|5'UTR|Start=1|Length=").append(seq_region.length());
			fasta.append("|CodonStart|").append(cds_start).append("|CdsLength|").append(cds_length).append("\n");
			
			arr[0] = Integer.toString(1);
			arr[1] = Integer.toString(cds_start-1);
			
		} else {
			seq_region = mRNA_seq.substring(cds_start-1, cds_end);
			fasta.append(">gi|").append(gi).append("|miGLE|CDS");
			fasta.append("|CodonStart|").append(cds_start).append("|CdsLength|").append(cds_length).append("\n");
			
			arr[0] = Integer.toString(1);
			arr[1] = Integer.toString(cds_length);
		}
		
//		if(region.equals("3utr")){
//			
//			seq_region = mRNA_seq.substring(cds_end, length);
//			fasta.append(">").append(accn).append("|miGLE_3'UTR");
//			fasta.append("|StartCodon|").append("0").append("|Cds_Length|").append(cds_length).append("\n");
//			
////			arr[0] = Integer.toString(cds_end + 1);
//			arr[1] = Integer.toString(seq_region.length()+cds_length);
//			
//			arr[0] = Integer.toString(cds_length+1);
////			arr[1] = Integer.toString(cds_end);
//			
//		} else if (region.equals("5utr")){
//			
//			seq_region = mRNA_seq.substring(0,cds_start-1);
//			fasta.append(">").append(accn).append("|miGLE_5'UTR");
//			fasta.append("|StartCodon|").append("0").append("|Cds_Length|").append((cds_start-1)).append("\n");
//			
//			arr[0] = Integer.toString(1);
//			arr[1] = Integer.toString(cds_start-1);
//			
//		} else {
//			seq_region = mRNA_seq.substring(cds_start-1, cds_end);
//			fasta.append(">").append(accn).append("|miGLE_CDS");
//			fasta.append("|StartCodon|").append("0").append("|Cds_Length|").append(cds_length).append("\n");
//			
//			arr[0] = Integer.toString(cds_start);
//			arr[1] = Integer.toString(cds_end);
//		}

		for (int i = 0; i < seq_region.length(); i++) {
			fasta.append(seq_region.charAt(i));
			if ((i + 1) % 60 == 0) {
				fasta.append("\n");
			}
		}
		
		arr[2] = fasta.toString();
		


//		System.out.println(arr[0]);
//		System.out.println(arr[1]);
//		System.out.println(arr[2]
		
		return arr;
	}
}
